﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebChamCong
{
    public partial class CreateAccountFrm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ddl_NameMs.Visible = false;
            if (!Page.IsPostBack)
            {
                LoadDepartmentName();
                ddl_NameMs.Visible = true;
                LoadNameUser(int.Parse(ddl_DepartmentID.Items[0].Value));
            }
           
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
        protected void LoadDepartmentName()
        {
            List<DTO.BoPhanDTO> departments = BUS.BoPhanBUS.GetBoPhans();

            this.ddl_DepartmentID.Items.Clear();

            foreach (var departItem in departments)
            {
                this.ddl_DepartmentID.Items.Add(new ListItem(departItem.DepartmentName, departItem.DepartmentID.ToString()));
            }

            this.ddl_DepartmentID.SelectedIndex = 0;

        }
        protected void LoadNameUser(int DepartID)
        {
            int localDepart = DepartID;
            this.ddl_NameMs.Items.Clear();
           List<DTO.NhanVienDTO> NameCp = BUS.NhanVienBUS.GetMsDeparment(localDepart);
           if (BUS.NhanVienBUS.CountNhanVienByDepartID(localDepart)>0)
           {
               foreach (var NameCp1 in NameCp)
               {
                   this.ddl_NameMs.Items.Add(new ListItem(NameCp1.FullName, NameCp1.UserId.ToString()));
               }
               this.ddl_NameMs.SelectedIndex = 0;
           }
           else
           { this.ddl_NameMs.Items.Add("Không có nhân viên phòng ban này"); }
        }
        protected void ddl_DepartmentID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddl_NameMs.Visible = true;
           
            LoadNameUser(int.Parse(ddl_DepartmentID.SelectedValue));
            
        }

        protected void BtCreateAccount_Click(object sender, EventArgs e)
        {
            if (txtlaplaimk.Text == "" || txtmatkhau.Text == "" || txtmatkhau.Text != txtlaplaimk.Text)
            {
                MessageBox("Việc xác nhận mật khẩu của bạn không trùng khớp");

            }
            else
            {
                Boolean kt = BUS.AccountBUS.createAccLogin(int.Parse(ddl_NameMs.SelectedValue), BUS.AccountBUS.MaHoaMD5(txtmatkhau.Text), 0);
                if (kt)
                {
                    bool kt1 = BUS.UserRolesBUS.createRoleUser(int.Parse(ddl_NameMs.SelectedValue));
                    if (kt1)
                    {
                        MessageBox("đăng kí thành công");
                    }
                    
                }
            }
        }

       
    }
}